package com.cci.kangdao.common;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;

/**
 * @author William Du
 * @date 2019/9/11
 */
@Configuration
public class ScheduledConfiguration implements SchedulingConfigurer {
    @Override
    public void configureTasks(ScheduledTaskRegistrar scheduledTaskRegistrar) {
        scheduledTaskRegistrar.setScheduler(threadPoolTaskScheduler());
    }

    @Bean
    public ThreadPoolTaskScheduler threadPoolTaskScheduler() {
        ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
        // 设置执行定时任务的线程数量
        scheduler.setPoolSize(20);
        // 线程池关闭的时候等待所有任务都完成
        scheduler.setWaitForTasksToCompleteOnShutdown(false);
        // 线程池中任务的等待时间，如果超过该等待时间还没有销毁就强制销毁
        //scheduler.setAwaitTerminationSeconds(60);
        return scheduler;
    }
}
